package WebTopN6;

import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class WebTopNMapper extends Mapper<LongWritable, Text, WebBean, FloatWritable> {
    WebBean outKey = new WebBean();
    FloatWritable outValue = new FloatWritable();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, WebBean, FloatWritable>.Context context) throws IOException, InterruptedException {

        String[] fields = value.toString().split("\t");
        if (fields.length ==2 ) {
            try {
            String province = fields[0].split(",")[0].split("=")[1];
            String year = fields[0].split(",")[1].split("=")[1];

            float web_num = Float.parseFloat(fields[1]);

            outKey.set(province, year, web_num);
            outValue.set(web_num);
            context.write(outKey, outValue);

        } catch (NumberFormatException e) {
                // 记录日志或者进行其他错误处理，这里简单打印错误信息
                System.err.println("数据格式解析错误，行数据: " + value.toString());
            }
        } else {
            // 记录日志或者进行其他错误处理，这里简单打印错误信息
            System.err.println("数据格式不符合预期，行数据: " + value.toString());
        }
        }
    }

